using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        //private DataGridView dGVmarkPoint;
        private ComputeRotationMartix rotationMatrix;
        private gjpingyi gjpy;

        public Form2()
        {
            InitializeComponent();
            dGVMarkPoint.RowHeadersVisible = false;
            dGVOriginalPoint.RowHeadersVisible = false;
            dGVfixedPoint.RowHeadersVisible = false;
            dGVRotation.RowHeadersVisible = false;
            rotationMatrix = new ComputeRotationMartix();  // 实例化 PointRotator 类
            gjpy = new gjpingyi();

            // 订阅 RowsAdded 事件
            dGVMarkPoint.RowsAdded += dGVMarkPoint_RowsAdded;
            // dGVMarkPoint.RowsAdded += dGV1_RowsAdd;
            //dGVmarkPoint = new DataGridView();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
        public void UpdateDataGrid(string[] data)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<string[]>(UpdateDataGrid), data);
            }
            else
            {
                if (dGVMarkPoint != null)
                {
                    dGVMarkPoint.Rows.Clear();
                    dGVMarkPoint.Rows.Add(data); // 将数据直接添加到 DataGridView 的行中
                }
                else
                {
                    MessageBox.Show("DataGridView 未初始化。");
                }
            }
        }

        private void dGVMarkPoint_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            // 清空 dGVRotation 中的旧数据
            dGVRotation.Rows.Clear();
            dataGridView1.Rows.Clear();

            // 获取新添加的行
            for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++)
            {
                DataGridViewRow row = dGVMarkPoint.Rows[i];

                // 确保行中有 θ 数据
                if (row.Cells["offsetheta"].Value != null)
                {
                    double dX = Convert.ToDouble(row.Cells["offsetX"].Value);
                    double dy = Convert.ToDouble(row.Cells["offsetY"].Value);
                    double theta = Convert.ToDouble(row.Cells["offsetheta"].Value);

                    // 计算所有固定点的旋转结果
                    var rotatedPoints = rotationMatrix.RotateFixedPoints(theta, clockwise: false);

                    // 计算所有固定点的旋转结果
                    var rotatedPoint = gjpy.RotateAndTranslateFixedPoints(theta, dX,dy, clockwise: false);

                    // 将每个旋转结果添加到 dGVRotation 中
                    foreach (var point in rotatedPoints)
                    {
                        dGVRotation.Rows.Add(point.Item1, point.Item2);
                    }

                    foreach (var point in rotatedPoint)
                    {
                        dataGridView1.Rows.Add(point.Item1, point.Item2);
                    }
                }
            }
        }

        //private void dGV1_RowsAdd(object sender, DataGridViewRowsAddedEventArgs e)
        //{
        //    // 清空 dGVRotation 中的旧数据
        //    dataGridView1.Rows.Clear();
        //    // 获取新添加的行
        //    for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++)
        //    {
        //        DataGridViewRow row = dataGridView1.Rows[i];

        //        // 确保行中有 θ 数据
        //        if (row.Cells["offsetheta"].Value != null)
        //        {
        //            double theta = Convert.ToDouble(row.Cells["offsetheta"].Value);

        //            // 计算所有固定点的旋转结果
        //            var rotatedPoints = gjpy.RotateFixedPoints(theta, clockwise: false);

        //            // 将每个旋转结果添加到 dGVRotation 中
        //            foreach (var point in rotatedPoints)
        //            {
        //                dataGridView1.Rows.Add(point.Item1, point.Item2);
        //            }
        //        }
        //    }
        //}

        private void dGVOriginalPoint_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dGVMarkPoint_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void groupBox4_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox5_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void dGVRotation_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}